package ext.opensource.netty.server.httpsocket;

import java.io.InputStream;
import java.util.Map;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

public class HttpResourceFile extends BaseHttpResource {

	@Override
	protected ByteBuf buildRes(final String resPath, final Map<String, Object> parameters) {
		try {
			final InputStream inputStream = HttpResourceFile.class
					.getResourceAsStream("/" + this.getRootDir() + resPath);
			if (inputStream != null) {
				final int available = inputStream.available();
				if (available != 0) {
					final byte[] bytes = new byte[available];
					inputStream.read(bytes);
					return ByteBufAllocator.DEFAULT.buffer(bytes.length).writeBytes(bytes);
				}
			}
		} catch (final Exception e) {
		}
		return null;
	}
}
